package com.bigfish.gbs.controller;

import com.alibaba.fastjson.JSON;
import com.bigfish.gbs.dto.Result;
import com.bigfish.gbs.entity.Resource;
import com.bigfish.gbs.enums.ResultEnum;
import com.bigfish.gbs.service.ResourceService;
import com.bigfish.gbs.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.util.ResultSetUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Author Joycessh
 * @Description Api管理相关接口
 * @Date 12/13/18 11:42 AM
 */
@Api(value = "API管理", description = "API资源的增删改查接口")
@RestController
@RequestMapping("/resource")
public class ResourceController extends BasicAction {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceController.class);

    @Autowired
    private ResourceService resourceService;

    /**
     * 获取Api List
     * @author Joycessh
     * @param currentPage
	 * @param pageSize
     * @return com.bigfish.gbs.dto.Result
     * @date 12/13/18 2:46 PM
     */
    @ApiOperation(value = "获取API List", httpMethod = "GET")
    @GetMapping("/api")
    public Result getApiList(@RequestParam Integer currentPage,
                             @RequestParam Integer pageSize) {

        PageHelper.startPage(currentPage, pageSize);
        List<Resource> resources = resourceService.getApiList();
        PageInfo pageInfo = new PageInfo(resources);
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("apis", pageInfo);
    }

    /**
     * 方法说明
     * @author Joycessh
     * @param map
     * @return com.bigfish.gbs.dto.Result
     * @date 12/15/18 12:09 PM
     */
    @ApiOperation(value = "增加API", httpMethod = "POST")
    @PostMapping("/api")
    public Result addApi(@RequestBody HashMap map) {       // @RequestBody能把简单json结构参数转换成实体类,key必须与实体类属性对应,格式 {key: value,}
                                                           // 如果传的是params: {key: vaule,},可以用map接收之后自行转换
        LOGGER.info("add resource start");
        Resource api = JSON.parseObject(JSON.toJSONString(map.get("params")), Resource.class);
        Boolean flag = resourceService.addApi(api);
        if (flag) {
            return ResultUtil.retFormat(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.retFormat(ResultEnum.FAIL);
        }
    }

    /**
     * 更新API
     * @author Joycessh
     * @param map
     * @return com.bigfish.gbs.dto.Result
     * @date 12/15/18 12:01 PM
     */
    @ApiOperation(value = "修改API", httpMethod = "PUT")
    @PutMapping("/api")
    public Result updateApi(@RequestBody HashMap map) {         // map接收RequestBody正文内容
        LOGGER.info("update resource start");
//        System.out.println(map.get("params").toString());
        Resource api = JSON.parseObject(JSON.toJSONString(     // 用fastjson将params转为jsonstring，再用parseObject转为实体类对象
                map.get("params")), Resource.class);
//        System.out.println("id:" + api.getId());
        Boolean flag = resourceService.updateApi(api);
        if (flag) {
            return ResultUtil.retFormat(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.retFormat(ResultEnum.FAIL);
        }
    }

    /**
     * 删除API
     * @author Joycessh
     * @param apiId
     * @return com.bigfish.gbs.dto.Result
     * @date 12/15/18 12:01 PM
     */
    @ApiOperation(value = "删除API", httpMethod = "DELETE")
    @DeleteMapping("/api")
    public Result deleteApi(@RequestParam Integer apiId) {
        Boolean flag = resourceService.deleteApi(apiId);
        if (flag) {
            return ResultUtil.retFormat(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.retFormat(ResultEnum.FAIL);
        }
    }
}
